package com.ywwxhz.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ywwxhz.Util.UIKit;
import com.ywwxhz.model.AppInfo;
import com.ywwxhz.taskmgr.R;

import java.util.ArrayList;
import java.util.List;

//自定义适配器类，提供给listView的自定义view
public class ProcessListAdapter extends BaseAdapter {

    LayoutInflater infater = null;
    Context context;
    private List<AppInfo> mlistAppInfo = null;

    public ProcessListAdapter(Context context) {
        mlistAppInfo = new ArrayList<AppInfo>();
        this.context = context;
        this.infater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public List<AppInfo> getDataSet() {
        return this.mlistAppInfo;
    }

    public void setDataSet(List<AppInfo> apps) {
        List<AppInfo> old = this.mlistAppInfo;
        this.mlistAppInfo = apps;
        old.clear();
    }

    @Override
    public int getCount() {
        return mlistAppInfo.size();
    }

    @Override
    public Object getItem(int position) {
        return mlistAppInfo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertview, ViewGroup arg2) {
        View view = null;
        ViewHolder holder = null;
        if (convertview == null || convertview.getTag() == null) {
            view = infater.inflate(R.layout.tasklist_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            view = convertview;
            holder = (ViewHolder) convertview.getTag();
        }
        AppInfo appInfo = (AppInfo) getItem(position);
        holder.appIcon.setImageDrawable(appInfo.getAppIcon());
        holder.ProcName.setText(appInfo.getProcessName());
        holder.ProcessMemSize.setText(appInfo.getMemSize());
        switch (appInfo.getFlag()) {
            case 0:
                holder.AppLabel.setText(UIKit.setTextColor(appInfo.getAppLabel(),
                        67, 191, 207));
                break;
            case 1:
                holder.AppLabel.setText(UIKit.setTextColor(appInfo.getAppLabel(),
                        104, 201, 104));
                break;
            case 2:
                holder.AppLabel.setText(UIKit.setTextColor(appInfo.getAppLabel(),
                        235, 148, 80));
                break;
            default:
                holder.AppLabel.setText(appInfo.getAppLabel());
                break;
        }
        return view;
    }

    class ViewHolder {
        ImageView appIcon;
        TextView AppLabel;
        TextView ProcName;
        TextView ProcessMemSize; // 进程占用内存大小

        public ViewHolder(View view) {
            this.appIcon = (ImageView) view.findViewById(R.id.appicon);
            this.AppLabel = (TextView) view.findViewById(R.id.appname);
            this.ProcName = (TextView) view.findViewById(R.id.ProcName);
            this.ProcessMemSize = (TextView) view.findViewById(R.id.mem);
        }
    }
}